package com.example.myanimator.mcanvas.SurfaceViewDemo

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.SurfaceView

/**
 * Created by 2021/4/24
 */
class ViewGesturePath : SurfaceView {
    private var mPaint: Paint? = null
    private var mPath: Path? = null

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {
        init()
    }

    private fun init() {
        setWillNotDraw(false)
        mPaint = Paint()
        mPaint!!.style = Paint.Style.STROKE
        mPaint!!.strokeWidth = 5f
        mPaint!!.color = Color.RED
        mPath = Path()
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        val x = event.x.toInt()
        val y = event.y.toInt()
        if (event.action == MotionEvent.ACTION_DOWN) {
            mPath!!.moveTo(x.toFloat(), y.toFloat())
            return true
        } else if (event.action == MotionEvent.ACTION_MOVE) {
            mPath!!.lineTo(x.toFloat(), y.toFloat())
        }
        postInvalidate()
        Log.d("tgw", "invalidate")
        return super.onTouchEvent(event)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawPath(mPath!!, mPaint!!)
        Log.d("tgw", "ondraw")
    }
}